#include <stdlib.h>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <time.h>

int main(int argc, char **argv)
{
  CvCapture *capture = 0;
  IplImage *frame = 0;
  int key = 0;

  /* initialize camera */

  capture = cvCaptureFromCAM(0);

  if(!capture) {
    fprintf( stderr, "Cannot open initialize webcam!\n");
    return 1;
  }

  cvNamedWindow( "result", CV_WINDOW_AUTOSIZE);
  clock_t start = clock();
  int frame_count = 0;
  
  //  for(int i = 0; i < 201; ++i){
  //printf("Hi")
  //cvQueryFrame(capture);
  //  }
  
  while( key != 'q'){
    /* get a frame */
    frame_count++;
    frame = cvQueryFrame( capture);

    /* always check */
    if (!frame) break;

    /* display current frame */
    cvShowImage( "result", frame);

    /* exit if user press 'q' */
    key = cvWaitKey(1);
    //    printf("Frame Count: %d\n", frame_count);
    printf("%f\n", ((double)CLOCKS_PER_SEC*frame_count/((clock() - start))));
    fflush(stdout);
  }

  /* free memory */
  cvDestroyWindow( "result" );
  cvReleaseCapture( &capture);

  return 0;
}
